﻿Public Class oUser
    Public Function UpdateJsonUser(
                                       ByVal oUserID As String, ByVal userName As String, ByVal CompanyCode As String, ByVal UserPassword As String, _
                                       ByVal IsAdmin As Boolean, ByVal IsActive As Boolean, ByVal IsTrial As Boolean, ByVal Email As String, ByVal Phone As String, _
                                       ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {userName, CompanyCode, UserPassword, IsAdmin, IsActive, IsTrial, Email, Phone}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append(" INSERT INTO [oUser]")
                    query.Append(" ([UserName] ")
                    query.Append(" ,[CompanyCode] ")
                    query.Append(" ,[Password] ")
                    query.Append(" ,[IsAdmin] ")
                    query.Append(" ,[IsActive] ")
                    query.Append(" ,[IsTrial] ")
                    query.Append(" ,[Email] ")
                    query.Append(" ,[Phone]) ")
                    query.Append(" VALUES (@Param1,@Param2,@Param3,@Param4,@Param5,@Param6,@Param7,@Param8)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("UPDATE  [oUser] SET ")
                    query.Append("  [CompanyCode] = @Param1")
                    query.Append("  ,[Password] = @Param2")
                    query.Append("  ,[IsAdmin] = @Param3")
                    query.Append("  ,[IsActive] = @Param4")
                    query.Append("  ,[IsTrial] = @Param5")
                    query.Append("  ,[Email] = @Param6")
                    query.Append("  ,[Phone] = @Param7")
                    query.Append("  ,[UserName] = @Param8")
                    query.Append(" WHERE [UserID] = @Param9")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {CompanyCode, UserPassword, IsAdmin, IsActive, IsTrial, Email, Phone, userName, oUserID})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function UpdateUser(
                                         ByVal data As DataSet, ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            For Each row As DataRow In data.Tables("oUser").Rows
                Dim errMsg As String = connect.setSQLDB(databaseName)
                If errMsg.Length = 0 Then
                    Dim params = New Object() {row("UserName"), row("CompanyCode"), row("Password"), row("IsAdmin"), row("IsActive"), row("IsTrial"), row("Email"), row("Phone")}
                    Dim query As New StringBuilder
                    If isInsert = True Then
                        query.Append(" INSERT INTO [oUser]")
                        query.Append(" ([UserName] ")
                        query.Append(" ,[CompanyCode] ")
                        query.Append(" ,[Password] ")
                        query.Append(" ,[IsAdmin] ")
                        query.Append(" ,[IsActive] ")
                        query.Append(" ,[IsTrial] ")
                        query.Append(" ,[Email] ")
                        query.Append(" ,[Phone]) ")
                        query.Append("VALUES (@Param1,@Param2,@Param3,@Param4,@Param5,@Param6,@Param7,@Param8)")
                        Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                    Else
                        query.Append("UPDATE  [oUser] SET ")
                        query.Append("  [CompanyCode] = @Param1")
                        query.Append("  ,[Password] = @Param2")
                        query.Append("  ,[IsAdmin] = @Param3")
                        query.Append("  ,[IsActive] = @Param4")
                        query.Append("  ,[IsTrial] = @Param5")
                        query.Append("  ,[Email] = @Param6")
                        query.Append("  ,[Phone] = @Param7")
                        query.Append("  ,[UserName] = @Param8")
                        query.Append(" WHERE [UserID] = @Param9")
                        connect.Object_Execute_SAP(query.ToString(), New Object() {row("CompanyCode"), row("Password"), row("IsAdmin"), row("IsActive"), row("IsTrial"), row("Email"), _
                                                                        row("Phone"), row("UserName"), row("UserID")})
                    End If
                Else
                    Return errMsg
                End If
            Next
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function

    Public Function GetAllUser(ByVal dataBaseName As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oUser")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oUser T0 JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode ORDER BY UserID DESC")
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetUserByUserName(ByVal dataBaseName As String, ByVal userName As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oUser")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oUser T0 JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode WHERE UserName = @Param1", New Object() {userName})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetLogin(ByVal dataBaseName As String, ByVal userName As String, ByVal passWord As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oCompany")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oUser T0 LEFT JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode WHERE UserName = @Param1 AND Password = @Param2", New Object() {userName, passWord})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function ChangePassword(ByVal dataBaseName As String, ByVal userName As String, ByVal oldPassWord As String, ByVal newPassWord As String) As Integer
        Dim connect As New Connection()
        Try
            Dim dsUser As DataSet = GetLogin(dataBaseName, userName, oldPassWord)
            If dsUser IsNot Nothing AndAlso dsUser.Tables.Count > 0 AndAlso dsUser.Tables(0).Rows.Count > 0 Then
                Dim errMsg As String = connect.setSQLDB(dataBaseName)
                If errMsg.Length = 0 Then
                    Dim query As New StringBuilder
                    query.Append("UPDATE  [oUser] SET ")
                    query.Append("  [Password] = @Param1")
                    query.Append(" WHERE [UserName] = @Param2")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), New Object() {newPassWord, userName})
                    Return count
                End If
            Else
                Return -1
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class
